/*
 * cgolay-alert.js v0.0.2
 * ================================================
 * Copyright 2012 Claybox Ltd
 * 
 * v0.0.3 is main to set the alert options
 * type(string): error, info, success, warning
 * buttons: 0.Yes  1.Yes No
 * height: int
 * width: int
 * modal: true or false
 * show: the effect of the alert show
 * title: the title of messagebox
 * content: the content of the messagebox
 * 
 **/
cgolay = {};

!function (cgolay) {
    var alertQueue = [];
    Alert = function (option) {
        var elOverlay,
        configDispear = 3,
        self = this;
        
        this.settings = {};
        this.settings.modal = option.modal ? option.modal : false;
        this.settings.type = option.type;
        this.settings.buttons = option.buttons;
        this.settings.height = option.height;
        this.settings.width = option.width;
        this.settings.show = option.show;
        this.settings.content = option.content;
        this.settings.title = option.title;

        this.elContext = $('<div></div>').addClass('alert').addClass(this.settings.type);
        if(this.settings.modal && !$('#overlay').length){
            elOverlay = $('<div id="overlay"></div>').empty().appendTo($(document.body));
            elOverlay.width($(document).width()).height($(document).height());
        }
        if(this.settings.height != -1){
            this.elContext.css("height", this.settings.height);
        }
        if(this.settings.width != -1){
            this.elContext.css("width", this.settings.width);
        }
        
        var elClose = $('<button type="button" class="close">×</button>').appendTo(this.elContext);
        elClose.click(function(){
            close();
        });
        $('<h4 class="alert-heading"></h4>').html(this.settings.title).appendTo(this.elContext);
        $('<p></p>').html(this.settings.content).appendTo(this.elContext);
        $('<div class="clear"></div>').appendTo(this.elContext);
        var elBtns = $('<p class="btn-group"></p>').appendTo(this.elContext);
        $(this.settings.buttons).each(function(){
            var alertBtn = $('<a class="btn"></a>').text(this.text).appendTo(elBtns);
            if ($.isFunction(this.click)){
                var btnCb = this.click;
                alertBtn.click(function(){
                    var cbRtn = btnCb.apply(self.elContext, arguments);
                    if(cbRtn === false)return;
                    close();
                });
            }
        });
        //        this.elContext.appendTo(document.body);
        this.invoke = function(){
            if(this.settings.show == "show"){
                show();
            }else if(this.settings.show == "fade"){
                fade();
            }else if(this.settings.show == "slide"){
                slide();
            }else{
                show();
            }
        }
        if(alertQueue.length == 0){
            this.invoke();
        }
        function show() {
            self.elContext.appendTo($('body'));
        }
        function fade() {
            self.elContext.fadeIn(1000).appendTo($('body'));
        }
        function slide() {
            self.elContext.slideToggle("slow").appendTo($('body'));
        }
        function close() {
            if(self.settings.modal && alertQueue.length ==1){
                $('#overlay').remove();
            }
            if(self.settings.show == 'show'){
                self.elContext.remove();
            }else if(self.settings.show == 'fade'){
                self.elContext.fadeOut(1000);
            }else if(self.settings.show == 'slide'){
                self.elContext.slideToggle("slow");
            }
            alertQueue.shift();
            if(alertQueue.length > 0){
                alertQueue[0].invoke();
            }
        }
    }
    cgolay.alert = function (option) {
        var defaultOption = {
            type: "alert-info",
            buttons: [],
            height: -1,
            width: -1,
            modal: true,
            show: "show",
            content: "",
            title: ""
        }
        //default option 
        var applyOption = $.extend({} , defaultOption , option);
        var v2_alert = new Alert(applyOption);
        alertQueue.push(v2_alert);
        return v2_alert;
    }
    cgolay.alert.Constructor = Alert;
}(window.cgolay);

